- Area新增Store
- 複製Category資料夾與CategoryController
- Contorller用Store取代Category
- 修改Upsert
public StoreController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public IActionResult Index()
{
List<Store> objStoreList = _unitOfWork.Store.GetAll().ToList();
return View(objStoreList);
}
public IActionResult Upsert(int? id)
{
if (id == null || id == 0)
{
return View(new Store());
}
else
{
Store storeObj = _unitOfWork.Store.Get(u => u.Id == id);
return View(storeObj);
}
}
[HttpPost]
public IActionResult Upsert(Store storeObj)
{
if (ModelState.IsValid)
{
if (storeObj.Id == 0)
{
_unitOfWork.Store.Add(storeObj);
TempData["success"] = "分店新增成功!";
}
else
{
_unitOfWork.Store.Update(storeObj);
TempData["success"] = "分店更新成功!";
}
_unitOfWork.Save();
return RedirectToAction("Index");
}
return View(storeObj);
}
@model IEnumerable<ClothesMall.Models.Store>
<p>
<a asp-controller="Store" asp-action="Upsert" class="btn btn-primary">
<i class="bi bi-plus-circle"></i> 新增分店
</a>
</p>
<table class="table" id="storeTbl">
<thead>
<tr>
<th>@Html.DisplayNameFor(model => model.Name)</th>
<th>@Html.DisplayNameFor(model => model.Address)</th>
<th>@Html.DisplayNameFor(model => model.City)</th>
<th>@Html.DisplayNameFor(model => model.PhoneNumber)</th>
<th>@Html.DisplayNameFor(model => model.Description)</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td> @Html.DisplayFor(modelItem => item.Name)</td>
<td>@Html.DisplayFor(modelItem => item.Address)</td>
<td>@Html.DisplayFor(modelItem => item.City)</td>
<td>@Html.DisplayFor(modelItem => item.PhoneNumber)</td>
<td>@Html.DisplayFor(modelItem => item.Description)</td>
<td>
<a asp-action="Upsert" asp-route-id="@item.Id" class="btn btn-primary mx-2"><i class="bi bi-pencil-square"></i>Edit</a> |
<button type="button" class="btn btn-danger mx-2 delete-btn" data-id="@item.Id">
<i class="bi bi-trash-fill"></i>Delete
</button>
</td>
</tr>
}
</tbody>
</table>
@section Scripts {
<script>
$(document).ready(function () {
$(".delete-btn").click(function () {
var id = $(this).data("id");
if (confirm("Are you sure you want to delete this Store?")) {
$.ajax({
url: "@Url.Action("Delete", "Store")",
type: "POST",
data: { id: id },
success: function () {
location.reload();
}
});
}
});
});
</script>
}